﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;

namespace Validator.ViewModels
{
    public partial class StudentFormViewModel : ObservableRecipient, IRecipient<ValueChangedMessage<bool>>
    {
        [ObservableProperty]
        [NotifyPropertyChangedRecipients]
        string name;

        [ObservableProperty]
        [NotifyPropertyChangedRecipients]
        string _class;

        [ObservableProperty]
        [NotifyPropertyChangedRecipients]
        string phone;

        private bool allowNew;

        [RelayCommand(CanExecute = nameof(CanAddNew))]
        void Delete()
        {
            /*Name = string.Empty;
            Class = string.Empty;
            Phone = string.Empty;*/
            WeakReferenceMessenger.Default.Cleanup();
        }

        public override string ToString() => $"Name: {Name}, Class: {Class}, Phone: {Phone}";

        [RelayCommand(CanExecute = nameof(CanAddNew))]
        void AddNew()
        {
            WeakReferenceMessenger.Default.Send(new ValueChangedMessage<string>($"{Name},{Class},{Phone}"));
        }

        bool CanAddNew() => allowNew;

        public void Receive(ValueChangedMessage<bool> message)
        {
            allowNew = message.Value;
            // 通知命令的可执行状态更新
            AddNewCommand.NotifyCanExecuteChanged();
            DeleteCommand.NotifyCanExecuteChanged();
        }
    }
}
